
#include "stdafx.h"
#include "NMessageQueue.h"
#include "NSyncobject.h"


NMessageQueue::NMessageQueue() : m_hSemaphore(NULL)
{
	m_bOK = false;
	::InitializeCriticalSection( &m_CS );
	m_hSemaphore = CreateSemaphore( NULL, 0, 0x7fffffff, "");
	if(NULL == m_hSemaphore) return;
	m_bOK = true;
}

NMessageQueue::~NMessageQueue()
{
	if(NULL != m_hSemaphore){ ::CloseHandle( m_hSemaphore ); m_hSemaphore=NULL; }
	::DeleteCriticalSection( &m_CS );
}


void NMessageQueue::Put( const unsigned int in_Message, const unsigned int in_wParam, const unsigned int in_lParam)
{
	NSyncObject sync( m_CS );
	m_Queue.push_back( lcData( in_Message, in_wParam, in_lParam));
	::ReleaseSemaphore(m_hSemaphore,1,NULL);
}

NMessageQueue::RETG NMessageQueue::Get(void)
{
	NSyncObject sync(m_CS);
	if(m_Queue.empty()) return RETG( false, 0, 0, 0);

	const lcData data = m_Queue.front();	
	m_Queue.pop_front();
	return RETG( true, data.m_Message, data.m_wParam, data.m_lParam );
}


void NMessageQueue::Init(void)
{
	m_Queue.clear();
}
